package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.pegasus.teamwork.pegasus.entity.BussServiceClassic;
import cn.pegasus.teamwork.pegasus.entity.BussUserOpinion;
import cn.pegasus.teamwork.pegasus.mapper.BussServiceClassicMapper;
import cn.pegasus.teamwork.pegasus.service.BussServiceClassicService;
import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageHelper;
import cn.iyizhan.teamwork.base.util.PageObject;

/**
 * @author ryan.bian
 * @version 
 */
@Service("bussServiceClassicService")
public class BussServiceClassicServiceImpl extends BaseServiceImpl<BussServiceClassic, BussServiceClassicMapper> implements BussServiceClassicService {
	// 注入当前dao对象
    @Autowired
    private BussServiceClassicMapper bussServiceClassicMapper;

    public BussServiceClassicServiceImpl() {
        setMapperClass(BussServiceClassicMapper.class, BussServiceClassic.class);
    }

	@Override
	public PageObject<BussServiceClassic> PageList(Map<String, Object> map) {
		int totalData = bussServiceClassicMapper.getCount(map);
        PageHelper pageHelper = new PageHelper(totalData, map);
        List<BussServiceClassic> list = bussServiceClassicMapper.findListPage(pageHelper.getMap());
        PageObject<BussServiceClassic> pageObject = pageHelper.getPageObject();
        pageObject.setDataList(list);
		return pageObject;
	}

	@Override
	public List<BussServiceClassic> findAllList(Long id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		map.put("classicStatus", 0);
		return bussServiceClassicMapper.findAllList(map);
	}

	@Override
	public BussServiceClassic findClassicInfo(Long id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", id);
		return bussServiceClassicMapper.findClassicInfo(map);
	}

	@Override
	public void add(Long id, Map<String, Object> map) {
		BussServiceClassic classic = M2O(map);
		//获取自增ID
		long classicId = bussServiceClassicMapper.getClassicId()+1L;
		if(classic.getFid()==-1){
			classic.setClassicCode(String.valueOf(classicId));
		}else{
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("id", classic.getFid());
			BussServiceClassic b =bussServiceClassicMapper.findClassicInfo(params);
			classic.setClassicCode(b.getClassicCode()+"-"+String.valueOf(classicId));
		}
		classic.setId(classicId);
		classic.setClassicStatus(0);
		classic.setUpdateTime(new Date());
		classic.setCreateTime(new Date());
		classic.setCreateUserBy(id);
		classic.setUpdateUserBy(id);
		bussServiceClassicMapper.insertByEntity(classic);
	}

	@Override
	public void update(Long id, Map<String, Object> map) {
		BussServiceClassic classic = M2O(map);
		classic.setUpdateTime(new Date());
		classic.setUpdateUserBy(id);
		bussServiceClassicMapper.updateByEntity(classic);
	}

	@Override
	public int delete(Long id, long classicId) {
		BussServiceClassic classic = new BussServiceClassic();
		//查询是否有下级
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("fid", classicId);
		List<BussServiceClassic> list = bussServiceClassicMapper.findAllList(map);
		if(null != list && list.size() > 0){
			return 2;
		}
		classic.setId(classicId);
		classic.setClassicStatus(1);
		classic.setUpdateTime(new Date());
		classic.setUpdateUserBy(id);
		bussServiceClassicMapper.updateByEntity(classic);
		return 1;
	}
}
